package com.liwenfeng.huangputi;

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
/*
* 把菜单.txt文件读到内存集合中，排序输出到控制台，再写回文件中
* */
public class MenuSort {
    public static void main(String[] args) {
        System.out.println(MenuSort.class.getResource("/菜单.txt").getPath());
        try (
                BufferedReader br = new BufferedReader(new FileReader("src/main/resources/菜单.txt"));
                BufferedWriter bw = new BufferedWriter(new FileWriter("src/main/resources/菜单1.txt"));
        ) {
            ArrayList<String> menus = new ArrayList<>();
            String line;
            while ((line = br.readLine()) != null) {
                menus.add(line);
            }
            Collections.sort(menus);
            menus.stream().forEach(item -> {
                String[] split = item.split("-");
                System.out.println(split[0].length() == 4 ? split[1] : "  " + split[1]);
            });
            menus.forEach(item -> {
                try {
                    bw.write(item);
                    bw.newLine();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            });
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
